<template>
	<view class="content padding fafafa">
		<view class="padding ffffff positionr border_2 margin_bottom3" v-for="item in list.data" :key="item.id"
			@click="selectInvoiceLook(item)">
			<view class="color_ling text_w">
				{{item.invoice_title}}
			</view>
			<view class="margin_top text_five color_san" v-if="item.duty_paragraph">
				税号{{item.duty_paragraph}}
			</view>
			<view class="margin_top flexbetween paddingtb" style="border-top: 1rpx solid rgba(153, 153, 153, .1);">
				<view class="flexleft">
					<view class="checkSelect margin_right2" @click.stop="setDefaultInvoiceLook(item.id)"
						v-if="!item.is_default">
						<image src="/static/image/home/wiexuanzhong.png" mode="widthFix"></image>
					</view>
					<view class="checkSelect margin_right2" @click.stop="setDefaultInvoiceLook(item.id)"
						v-if="item.is_default">
						<image src="/static/image/home/xuanzhong.png" mode="widthFix"></image>
					</view>
					<view class="color_san text_b">
						设为默认
					</view>
				</view>
				<view class="flexleft">
					<view class="flexleft margin_right6" @click.stop="goAddInvoiceLook(item.id)">
						<image src="/static/image/mine/edit.png" class="editImage margin_right2" mode="widthFix"></image>
						<view class="text_b color_san">
							编辑
						</view>
					</view>
					<view class="flexleft" @click.stop="deleteInvoiceLook(item.id)">
						<image src="/static/image/mine/delete.png" class="deleteImage margin_right2" mode="widthFix"></image>
						<view class="text_b color_san">
							删除
						</view>
					</view>
				</view>
			</view>
			<view class="invoiceType">
				<text v-if="item.invoice_title_type==1">个人</text>
				<text v-if="item.invoice_title_type==2">单位</text>
			</view>
		</view>
		<view class="flexcenter placeholderBoxTop" v-if="!list.data.length">
			<image class="placeholderBox" src="/static/placeholderimg.png" mode="widthFix"></image>
		</view>
		<!-- 底部 -->
		<view class="footer ffffff padding">
			<view class="footerButton" @click="goAddInvoiceLook('')">
				+新增发票抬头
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				quan: false,
				page: 1,
				list: {
					data: []
				},
				isInvoice: 0,
			}
		},
		onLoad(options) {
			this.isInvoice = options.type
			console.log(this.isInvoice);
		},
		onShow() {
			this.getList()
		},
		methods: {
			getList(isPage, page) { //发票抬头列表
				let _this = this;
				httpRequest.request('/api/invoice/getlist', 'GET', {
					page: page || 1,
				}, false, false, true).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							_this.list = res.data
							_this.list.data = resList.data;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.getList(true, page);
			},
			changeclick() {
				this.quan = this.quan ? false : true
			},
			setDefaultInvoiceLook(id) { //设置默认发票抬头
				let _this = this;
				httpRequest.request('/api/invoice/setdefault', 'POST', {
					id
				}, false, false, true).then(res => {
					if (res.code == 1) {
						httpRequest.toast(res.msg);
						_this.getList()
					} else {
						httpRequest.toast(res.msg);
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			goAddInvoiceLook(id) { //新增、编辑发票抬头
				uni.navigateTo({
					url: "./addInvoiceLook?id=" + id
				})
			},
			deleteInvoiceLook(id) { //删除发票抬头
				let _this = this;
				httpRequest.modal('提示', '请再次确认', true, (resp) => {
					if(resp){
						httpRequest.request('/api/invoice/del', 'POST', {
							id
						}, false, false, true).then(res => {
							if (res.code == 1) {
								httpRequest.toast(res.msg);
								_this.getList()
							} else {
								httpRequest.toast(res.msg);
							}
						}).catch(err => {
							httpRequest.toast(err.msg);
						})
					}
				})
			},
			selectInvoiceLook(item) { //选择发票抬头
				if (this.isInvoice == 1) {
					return false
				}
				uni.$emit("invoiceLook", item)
				uni.navigateBack(1)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.editImage {
		width: 24rpx;
		height: 28rpx;
	}

	.deleteImage {
		width: 28rpx;
		height: 28rpx;
	}

	.checkSelect {
		width: 24rpx;
		height: 24rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.invoiceType {
		position: absolute;
		top: 0;
		right: 0;
		width: 115rpx;
		height: 44rpx;
		line-height: 44rpx;
		text-align: center;
		background: rgba(243, 39, 28, .1);
		border-radius: 0rpx 14rpx 0rpx 14rpx;
		font-size: 22rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #F3271C;
	}

	// .footer{
	// 	width: 100%;
	// 	box-sizing: border-box;
	// 	position: fixed;
	// 	bottom: 0;
	// 	left: 0;
	// 	.footerButton{
	// 		padding: 20rpx;
	// 		border-radius: 41rpx;
	// 		text-align: center;
	// 		background-color: rgba(243, 39, 28, 1);
	// 		font-size: 32rpx;
	// 		font-family: PingFang SC-Medium, PingFang SC;
	// 		font-weight: 500;
	// 		color: #FFFFFF;
	// 	}
	// }
</style>